package com.jh3502.vivo.function.message;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.function.messagedetail.MessageDetailActivity;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author:harisucici on 2018/11/7 10:19
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class MessageFragment extends BaseFragment<MessagePresenter.MessageView, MessagePresenter>
        implements MessagePresenter.MessageView, MessageInterface, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.msg_recycler)
    MyRecyclerView msgRecycler;
    @BindView(R.id.msg_swipe)
    SwipeRefreshLayout msgSwipe;
    List<Map<String, Object>> dataList = new ArrayList<>(2);
    private MessageAdapter messageAdapter;
    private LinearLayoutManager layoutMgr;


    @Override
    public MessagePresenter initPresenter() {
        return new MessagePresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        mPresenter.setMessageView(this);

        messageAdapter = new MessageAdapter(mContext, dataList);
        msgRecycler.setAdapter(messageAdapter);
        layoutMgr = msgRecycler.getLayoutManager();
        messageAdapter.setButtonSetOnclickDelete(position -> {
            Map<String, Object> body = new HashMap<>(2);
            body.put("id", dataList.get(position).get("id"));
            mPresenter.toDeleteMsg(body);
        });
        messageAdapter.setOnItemClickListener(s -> {
            //1：出差 2：报销 3：系统 4:授权
            String type = (String) dataList.get(s).get("type");
            View view = layoutMgr.getChildAt(s - (int) layoutMgr.getChildAt(0).getTag());
            TextView point = view.findViewById(R.id.item_message_point);
            point.setVisibility(View.GONE);

            Bundle bundle = new Bundle();
            bundle.putString("type", (String) dataList.get(s).get("type"));
            bundle.putString("id", (String) dataList.get(s).get("id"));
            bundle.putString("busId", (String) dataList.get(s).get("busId"));
            Utils.getInstance().startActivity(mActivity, MessageDetailActivity.class, bundle);
        });
    }


    @Override
    protected void initData() {
        mPresenter.toFindMsg();

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnVisible() {

    }

    @Override
    protected void doOnInvisible() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
        msgSwipe.setRefreshing(false);

    }

    @Override
    public void requestFailure(Throwable t) {

    }


    @Override
    public void setEdit(boolean b) {
        messageAdapter.setIsEdit(b);
        messageAdapter.notifyDataSetChanged();
    }


    @Override
    public void onRefresh() {
        dataList.clear();

    }


    @Override
    public void findMsgSuccess(List<Map<String, Object>> datas) {
        msgSwipe.setRefreshing(false);
        dataList.addAll(datas);
        messageAdapter.notifyDataSetChanged();
    }

    @Override
    public void deleteMsgSuccess() {
        showShortToast("删除成功");
        dataList.clear();
        mPresenter.toFindMsg();
    }
}
